package com.eqdd.yiqidian.web;

import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.WebActivityCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.gamerole.richtextview.RichEditor;
import com.google.gson.Gson;

import java.util.regex.Pattern;

public class WebActivity extends BaseActivity {
    private WebActivityCustom dataBinding;
    private boolean isLogin;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        isKeyBoardJustPan = false;
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_web);
    }

    @Override
    public void initData() {

        String title = getIntent().getStringExtra(Config.TITLE);
        String url = getIntent().getStringExtra(Config.URL);
        isLogin = getIntent().getBooleanExtra(Config.IS_LOGIN, false);
        System.out.println(url);
        if (!TextUtils.isEmpty(url)) {
            boolean matches = Pattern.matches("^http[s]?://[\\S]*?$", url.toLowerCase());
            if (!matches) {
                initTopTitleBar(View.VISIBLE, TextUtils.isEmpty(title) ? "信息" : title);
                dataBinding.wvContent.setVisibility(View.GONE);
                dataBinding.tvContent.setVisibility(View.VISIBLE);
                dataBinding.tvContent.setText(url);
            } else {
//                Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//                startActivity(in);
//                finish();
                initWeb(title, url);
            }
        } else {
            initWeb("易企点", Config.URL_ABOUT_US);
        }

    }

    private void initWeb(String title, String url) {
        initTopTitleBar(View.VISIBLE, TextUtils.isEmpty(title) ? "易企点" : title);

        //获取websetings 设置
        WebSettings settings = dataBinding.wvContent.getSettings();
        settings.setSupportZoom(true);
        //设置浏览器支持javaScript
        settings.setJavaScriptEnabled(true);
        //设置打开自带缩放按钮
        settings.setBuiltInZoomControls(true);

        // init webview settings
        settings.setAllowContentAccess(true);
        settings.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setSavePassword(false);
        settings.setSaveFormData(false);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
//        // 设置不可缩放
//        settings.setBuiltInZoomControls(false);
//        settings.setSupportZoom(false);
//        settings.setDisplayZoomControls(false);

        // 设置支持屏幕适配
//        settings.setUseWideViewPort(true);
//        settings.setLoadWithOverviewMode(true);
        // 进行跳转用户输入的url地址
        dataBinding.wvContent.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        dataBinding.wvContent.getSettings().setJavaScriptEnabled(true);
        dataBinding.wvContent.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        dataBinding.wvContent.loadUrl(url);
        dataBinding.wvContent.setWebChromeClient(new WebChromeClient());
        dataBinding.wvContent.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                // 传递参数调用

//                dataBinding.wvContent.loadUrl("javascript:onLogin(" + "'{\"iphoto\":\"https://www.eqid.top:8009/image/20171118/15286837836/20171118100250headimage.png\",\"uname\":\"15286837836\",\"upname\":\"Relax\",\"authen\":1,\"Guid\":\"33c6bdfc281c48c3871d85a2718620e9\",\"EQDCode\":\"l12345678\",\"addTime\":\"2017/10/16 16:11:18\",\"companyId\":14,\"company\":\"易企点\",\"departId\":251,\"department\":\"研讨部\",\"postId\":102,\"post\":\"负责人\",\"jobNumber\":\"yt171010002\",\"step\":3,\"username\":\"梁新帅\",\"isAdmin\":1,\"shiftId\":0,\"isleader\":true,\"workImage\":\"https://www.eqid.top:8009/WorkCircles/workImage.jpg\",\"Signature\":\"人生路远\",\"friendCircleImage\":\"https://www.eqid.top:8009/WorkCircles/workImage.jpg\",\"signEntryTime\":\"\"}'" + ")");
//                dataBinding.wvContent.loadUrl("javascript:test()");
                if (isLogin) {
                    dataBinding.wvContent.loadUrl("javascript:onLogin(" + "'" + new Gson().toJson(AppConstent.user) + "'" + ")");
                }

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

//                dataBinding.wvContent.loadUrl("javascript:test()");
//                dataBinding.wvContent.loadUrl("javascript:onLogin(" + "'{\"iphoto\":\"https://www.eqid.top:8009/image/20171118/15286837836/20171118100250headimage.png\",\"uname\":\"15286837836\",\"upname\":\"Relax\",\"authen\":1,\"Guid\":\"33c6bdfc281c48c3871d85a2718620e9\",\"EQDCode\":\"l12345678\",\"addTime\":\"2017/10/16 16:11:18\",\"companyId\":14,\"company\":\"易企点\",\"departId\":251,\"department\":\"研讨部\",\"postId\":102,\"post\":\"负责人\",\"jobNumber\":\"yt171010002\",\"step\":3,\"username\":\"梁新帅\",\"isAdmin\":1,\"shiftId\":0,\"isleader\":true,\"workImage\":\"https://www.eqid.top:8009/WorkCircles/workImage.jpg\",\"Signature\":\"人生路远\",\"friendCircleImage\":\"https://www.eqid.top:8009/WorkCircles/workImage.jpg\",\"signEntryTime\":\"\"}'" + ")");
                if (isLogin) {
                    dataBinding.wvContent.loadUrl("javascript:onLogin(" + "'" + new Gson().toJson(AppConstent.user) + "'" + ")");
                }
            }
        });
    }


    @Override
    public void setView() {

    }

    @Override
    public void onBackPressed() {
        if (dataBinding.wvContent.canGoBack()) {
            dataBinding.wvContent.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && dataBinding.wvContent.canGoBack()) {
            dataBinding.wvContent.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

}
